//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "Basket.h"
using namespace Cephei::QL::Experimental::Credit;
#include <gen/QL/Experimental/Credit/Pool.h>
#include <gen/QL/Experimental/Credit/DefaultProbKey.h>
#include <gen/QL/Experimental/Credit/RecoveryRateModel.h>
#include <gen/QL/Experimental/Credit/Loss.h>
#define HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Experimental::Credit::CBasket::CBasket (Cephei::Core::IVector<String^>^ names, Cephei::Core::IVector<Double>^ notionals, Cephei::QL::Experimental::Credit::IPool^ pool, Cephei::Core::IVector<Cephei::QL::Experimental::Credit::IDefaultProbKey^>^ defaultKeys, Cephei::Core::IVector<Cephei::QL::Experimental::Credit::IRecoveryRateModel^>^ rrModels, Microsoft::FSharp::Core::FSharpOption<Double>^ attachmentRatio, Microsoft::FSharp::Core::FSharpOption<Double>^ detachmentRatio) 
{
	_pSpinlock = new boost::detail::spinlock ();
    CPool^ _Cpool;
    try
    {
#ifdef HANDLE
        _phBasket = NULL;
#endif
        names->Lock();
        INativeVector<String^>^ _NCInames = names->getFeature (NativeFeature::Value);
        CStringVector^ _NCnames = safe_cast<CStringVector^>(_NCInames);
        std::vector<std::string>& _names = static_cast<std::vector<std::string>&> (_NCnames->GetReference ());
        notionals->Lock();
        INativeVector<Double>^ _NCInotionals = notionals->getFeature (NativeFeature::Value);
        CDoubleVector^ _NCnotionals = safe_cast<CDoubleVector^>(_NCInotionals);
        std::vector<QuantLib::Real>& _notionals = static_cast<std::vector<QuantLib::Real>&> (_NCnotionals->GetReference ());
        _Cpool = safe_cast<CPool^> (pool);
        _Cpool->Lock();
        boost::shared_ptr<QuantLib::Pool> _pool = static_cast<boost::shared_ptr<QuantLib::Pool>> (_Cpool->GetShared ()); 
        defaultKeys ->Lock ();
        INativeVector<Cephei::QL::Experimental::Credit::IDefaultProbKey^>^ _NCIdefaultKeys = defaultKeys->getFeature (NativeFeature::Value);
        CDefaultProbKeyVector^ _NCdefaultKeys = safe_cast<CDefaultProbKeyVector^>(_NCIdefaultKeys);
        std::vector<QuantLib::DefaultProbKey>& _defaultKeys = static_cast<std::vector<QuantLib::DefaultProbKey>&> (_NCdefaultKeys->GetReference ());
        rrModels ->Lock ();
        INativeVector<Cephei::QL::Experimental::Credit::IRecoveryRateModel^>^ _NCIrrModels = rrModels->getFeature (NativeFeature::shared_ptr);
        CRecoveryRateModelVector^ _NCrrModels = safe_cast<CRecoveryRateModelVector^>(_NCIrrModels);
        std::vector<boost::shared_ptr<QuantLib::RecoveryRateModel> >& _rrModels = static_cast<std::vector<boost::shared_ptr<QuantLib::RecoveryRateModel> >&> (_NCrrModels->GetShared ());
        QuantLib::Real _attachmentRatio = 
            (Microsoft::FSharp::Core::FSharpOption<Double>::IsSome::get (attachmentRatio) ? (QuantLib::Real)ValueHelper::Convert (attachmentRatio->Value) : 0.0); //4
        QuantLib::Real _detachmentRatio = 
            (Microsoft::FSharp::Core::FSharpOption<Double>::IsSome::get (detachmentRatio) ? (QuantLib::Real)ValueHelper::Convert (detachmentRatio->Value) : 1.0); //4
        _ppBasket = new boost::shared_ptr<QuantLib::Basket> (new QuantLib::Basket ( _names,  _notionals,  _pool,  _defaultKeys,  _rrModels,  _attachmentRatio,  _detachmentRatio ));
        
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (names != nullptr) names->Unlock();    //not optional
        if (notionals != nullptr) notionals->Unlock();    //not optional
        if (_Cpool != nullptr) _Cpool->Unlock();
        if (defaultKeys != nullptr) defaultKeys->Unlock();    //not optional
        if (rrModels != nullptr) rrModels->Unlock();    //not optional
    }
}
Cephei::QL::Experimental::Credit::CBasket::CBasket () 
{
	_pSpinlock = new boost::detail::spinlock ();
    try
    {
#ifdef HANDLE
        _phBasket = NULL;
#endif
        _ppBasket = new boost::shared_ptr<QuantLib::Basket> (new QuantLib::Basket ( ));
        
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Experimental::Credit::CBasket::CBasket (boost::shared_ptr<QuantLib::Basket>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phBasket = NULL;
#endif
	_ppBasket = &childNative;
    
}
Cephei::QL::Experimental::Credit::CBasket::CBasket (QuantLib::Basket& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phBasket = NULL;
#endif
	_ppBasket = new boost::shared_ptr<QuantLib::Basket> (&childNative);
    
    _BasketOwner = owner;
    
}

Cephei::QL::Experimental::Credit::CBasket::CBasket (CBasket^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phBasket = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppBasket = new boost::shared_ptr<QuantLib::Basket> (copy->GetShared());
        
    }
}
Cephei::QL::Experimental::Credit::CBasket::CBasket (PLATFORM::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phBasket = NULL;
#endif
	if (!t->IsSubclassOf(CBasket::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Experimental::Credit::CBasket::CBasket (QuantLib::Handle<QuantLib::Basket>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phBasket = &childNative;
	_ppBasket = &static_cast<boost::shared_ptr<QuantLib::Basket>>(childNative.currentLink());
    
    _BasketOwner = owner;
}
Cephei::QL::Experimental::Credit::CBasket::CBasket (QuantLib::Handle<QuantLib::Basket> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phBasket = &childNative;
	_ppBasket = &static_cast<boost::shared_ptr<QuantLib::Basket>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::Experimental::Credit::CBasket::CBasket (QuantLib::Basket childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phBasket = NULL;
#endif
	_ppBasket = new boost::shared_ptr<QuantLib::Basket> (new QuantLib::Basket (childNative));
    
}
#endif

Cephei::QL::Experimental::Credit::CBasket::~CBasket ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppBasket != NULL)
    {
	    delete _ppBasket;
        _ppBasket = NULL;
    }
}
Cephei::QL::Experimental::Credit::CBasket::!CBasket ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppBasket != NULL)
    {
	    delete _ppBasket;
    }
}
QuantLib::Basket& Cephei::QL::Experimental::Credit::CBasket::GetReference ()
{
    if (_ppBasket == NULL) throw REFNEW NativeNullException ();
	return **_ppBasket;
}
boost::shared_ptr<QuantLib::Basket>& Cephei::QL::Experimental::Credit::CBasket::GetShared ()
{
    if (_ppBasket == NULL) throw REFNEW NativeNullException ();
	return *_ppBasket;
}
QuantLib::Basket* Cephei::QL::Experimental::Credit::CBasket::GetPointer ()
{
    if (_ppBasket == NULL) throw REFNEW NativeNullException ();
	return &**_ppBasket;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::Basket>& Cephei::QL::Experimental::Credit::CBasket::GetHandle ()
{
	if (_phBasket == NULL)
	{
		_phBasket = new Handle<QuantLib::Basket> (*_ppBasket);
	}
	return *_phBasket;
}
#endif
bool Cephei::QL::Experimental::Credit::CBasket::HasNative () 
{
	return (_ppBasket != NULL);
}

Double Cephei::QL::Experimental::Credit::CBasket::AttachmentAmount::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppBasket)->attachmentAmount ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Experimental::Credit::CBasket::AttachmentRatio::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppBasket)->attachmentRatio ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Experimental::Credit::CBasket::BasketLGD::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppBasket)->basketLGD ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Experimental::Credit::CBasket::BasketNotional::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppBasket)->basketNotional ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Experimental::Credit::CBasket::CumulatedLoss (DateTime startDate, DateTime endDate)
{
    try
    {
        QuantLib::Date _startDate = (QuantLib::Date)ValueHelper::Convert (startDate); //a
        QuantLib::Date _endDate = (QuantLib::Date)ValueHelper::Convert (endDate); //a
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppBasket)->cumulatedLoss ( _startDate,  _endDate );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Experimental::Credit::CBasket::DetachmentAmount::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppBasket)->detachmentAmount ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Experimental::Credit::CBasket::DetachmentRatio::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppBasket)->detachmentRatio ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::Core::IVector<Double>^ Cephei::QL::Experimental::Credit::CBasket::LGDs::get ()
{
    try
    {
    	std::vector<QuantLib::Real>& _rv = (std::vector<QuantLib::Real>&)(*_ppBasket)->LGDs ( );   
        Cephei::Core::IVector<Double>^ _nrv = REFNEW CoVector<Double> (REFNEW CDoubleVector (_rv));
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::Core::IVector<String^>^ Cephei::QL::Experimental::Credit::CBasket::Names::get ()
{
    try
    {
    	std::vector<std::string>& _rv = (std::vector<std::string>&)(*_ppBasket)->names ( );   
        Cephei::Core::IVector<String^>^ _nrv = REFNEW CoVector<String^> (REFNEW CStringVector (_rv));
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::Core::IVector<Double>^ Cephei::QL::Experimental::Credit::CBasket::Notionals::get ()
{
    try
    {
    	std::vector<QuantLib::Real>& _rv = (std::vector<QuantLib::Real>&)(*_ppBasket)->notionals ( );   
        Cephei::Core::IVector<Double>^ _nrv = REFNEW CoVector<Double> (REFNEW CDoubleVector (_rv));
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Experimental::Credit::IPool^ Cephei::QL::Experimental::Credit::CBasket::Pool::get ()
{
    try
    {
    	boost::shared_ptr<QuantLib::Pool> _rv = (boost::shared_ptr<QuantLib::Pool>)(*_ppBasket)->pool ( );   
        Cephei::QL::Experimental::Credit::CPool^ _nrv = REFNEW Cephei::QL::Experimental::Credit::CPool (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::Core::IVector<Double>^ Cephei::QL::Experimental::Credit::CBasket::Probabilities (DateTime d)
{
    try
    {
        QuantLib::Date _d = (QuantLib::Date)ValueHelper::Convert (d); //a
    	std::vector<QuantLib::Real> _rv = (std::vector<QuantLib::Real>)(*_ppBasket)->probabilities ( _d );   
        Cephei::Core::IVector<Double>^ _nrv = REFNEW CoVector<Double> (REFNEW CDoubleVector (_rv));
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Experimental::Credit::CBasket::RemainingAttachmentAmount (DateTime startDate, DateTime endDate)
{
    try
    {
        QuantLib::Date _startDate = (QuantLib::Date)ValueHelper::Convert (startDate); //a
        QuantLib::Date _endDate = (QuantLib::Date)ValueHelper::Convert (endDate); //a
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppBasket)->remainingAttachmentAmount ( _startDate,  _endDate );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Experimental::Credit::CBasket::RemainingAttachmentRatio (DateTime startDate, DateTime endDate)
{
    try
    {
        QuantLib::Date _startDate = (QuantLib::Date)ValueHelper::Convert (startDate); //a
        QuantLib::Date _endDate = (QuantLib::Date)ValueHelper::Convert (endDate); //a
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppBasket)->remainingAttachmentRatio ( _startDate,  _endDate );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Experimental::Credit::CBasket::RemainingDetachmentAmount (DateTime startDate, DateTime endDate)
{
    try
    {
        QuantLib::Date _startDate = (QuantLib::Date)ValueHelper::Convert (startDate); //a
        QuantLib::Date _endDate = (QuantLib::Date)ValueHelper::Convert (endDate); //a
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppBasket)->remainingDetachmentAmount ( _startDate,  _endDate );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Experimental::Credit::CBasket::RemainingDetachmentRatio (DateTime startDate, DateTime endDate)
{
    try
    {
        QuantLib::Date _startDate = (QuantLib::Date)ValueHelper::Convert (startDate); //a
        QuantLib::Date _endDate = (QuantLib::Date)ValueHelper::Convert (endDate); //a
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppBasket)->remainingDetachmentRatio ( _startDate,  _endDate );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::Core::IVector<String^>^ Cephei::QL::Experimental::Credit::CBasket::RemainingNames (DateTime startDate, DateTime endDate)
{
    try
    {
        QuantLib::Date _startDate = (QuantLib::Date)ValueHelper::Convert (startDate); //a
        QuantLib::Date _endDate = (QuantLib::Date)ValueHelper::Convert (endDate); //a
    	std::vector<std::string> _rv = (std::vector<std::string>)(*_ppBasket)->remainingNames ( _startDate,  _endDate );   
        Cephei::Core::IVector<String^>^ _nrv = REFNEW CoVector<String^> (REFNEW CStringVector (_rv));
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Experimental::Credit::CBasket::RemainingNotional (DateTime startDate, DateTime endDate)
{
    try
    {
        QuantLib::Date _startDate = (QuantLib::Date)ValueHelper::Convert (startDate); //a
        QuantLib::Date _endDate = (QuantLib::Date)ValueHelper::Convert (endDate); //a
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppBasket)->remainingNotional ( _startDate,  _endDate );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::Core::IVector<Double>^ Cephei::QL::Experimental::Credit::CBasket::RemainingNotionals (DateTime startDate, DateTime endDate)
{
    try
    {
        QuantLib::Date _startDate = (QuantLib::Date)ValueHelper::Convert (startDate); //a
        QuantLib::Date _endDate = (QuantLib::Date)ValueHelper::Convert (endDate); //a
    	std::vector<QuantLib::Real> _rv = (std::vector<QuantLib::Real>)(*_ppBasket)->remainingNotionals ( _startDate,  _endDate );   
        Cephei::Core::IVector<Double>^ _nrv = REFNEW CoVector<Double> (REFNEW CDoubleVector (_rv));
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::Core::IVector<Cephei::QL::Experimental::Credit::ILoss^>^ Cephei::QL::Experimental::Credit::CBasket::ScenarioIncrementalBasketLosses::get ()
{
    try
    {
    	std::vector<QuantLib::Loss> _rv = (std::vector<QuantLib::Loss>)(*_ppBasket)->scenarioIncrementalBasketLosses ( );   
        CoVector<Cephei::QL::Experimental::Credit::ILoss^>^ _nrv = REFNEW CoVector<Cephei::QL::Experimental::Credit::ILoss^>(REFNEW CLossVector (_rv));
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::Core::IVector<Cephei::QL::Experimental::Credit::ILoss^>^ Cephei::QL::Experimental::Credit::CBasket::ScenarioIncrementalTrancheLosses (Microsoft::FSharp::Core::FSharpOption<DateTime>^ startDate, Microsoft::FSharp::Core::FSharpOption<DateTime>^ endDate)
{
    try
    {
        QuantLib::Date _startDate = 
            (Microsoft::FSharp::Core::FSharpOption<DateTime>::IsSome::get (startDate) ? (QuantLib::Date)ValueHelper::Convert (startDate->Value) : QuantLib::Date::minDate()); //9a
        QuantLib::Date _endDate = 
            (Microsoft::FSharp::Core::FSharpOption<DateTime>::IsSome::get (endDate) ? (QuantLib::Date)ValueHelper::Convert (endDate->Value) : QuantLib::Date::maxDate()); //9a
    	std::vector<QuantLib::Loss> _rv = (std::vector<QuantLib::Loss>)(*_ppBasket)->scenarioIncrementalTrancheLosses ( _startDate,  _endDate );   
        CoVector<Cephei::QL::Experimental::Credit::ILoss^>^ _nrv = REFNEW CoVector<Cephei::QL::Experimental::Credit::ILoss^>(REFNEW CLossVector (_rv));
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Experimental::Credit::CBasket::ScenarioTrancheLoss (DateTime endDate)
{
    try
    {
        QuantLib::Date _endDate = (QuantLib::Date)ValueHelper::Convert (endDate); //a
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppBasket)->scenarioTrancheLoss ( _endDate );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
UInt64 Cephei::QL::Experimental::Credit::CBasket::Size::get ()
{
    try
    {
    	QuantLib::Size _rv = (QuantLib::Size)(*_ppBasket)->size ( );   
        UInt64 _nrv = _rv; //c1
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Experimental::Credit::CBasket::TrancheNotional::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppBasket)->trancheNotional ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Experimental::Credit::IBasket^ Cephei::QL::Experimental::Credit::CBasket::UpdateScenarioLoss (Microsoft::FSharp::Core::FSharpOption<Boolean>^ zeroRecovery)
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        bool _zeroRecovery = 
            (Microsoft::FSharp::Core::FSharpOption<Boolean>::IsSome::get (zeroRecovery) ? (bool)ValueHelper::Convert (zeroRecovery->Value) : false); //9a
    	(*_ppBasket)->updateScenarioLoss ( _zeroRecovery );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::Core::IVector<Cephei::QL::Experimental::Credit::IDefaultProbKey^>^ Cephei::QL::Experimental::Credit::CBasket::DefaultKeys::get ()
{
    try
    {
    	std::vector<QuantLib::DefaultProbKey>& _rv = (std::vector<QuantLib::DefaultProbKey>&)(*_ppBasket)->defaultKeys ( );   
        CoVector<Cephei::QL::Experimental::Credit::IDefaultProbKey^>^ _nrv = REFNEW CoVector<Cephei::QL::Experimental::Credit::IDefaultProbKey^>(REFNEW CDefaultProbKeyVector (_rv, this));
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::Core::IVector<Cephei::QL::Experimental::Credit::IRecoveryRateModel^>^ Cephei::QL::Experimental::Credit::CBasket::RecoveryModels::get ()
{
    try
    {
    	std::vector<boost::shared_ptr<QuantLib::RecoveryRateModel> >& _rv = (std::vector<boost::shared_ptr<QuantLib::RecoveryRateModel> >&)(*_ppBasket)->recoveryModels ( );   
        CoVector<Cephei::QL::Experimental::Credit::IRecoveryRateModel^>^ _nrv = REFNEW CoVector<Cephei::QL::Experimental::Credit::IRecoveryRateModel^>(REFNEW CRecoveryRateModelVector (_rv, this));
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::Core::IVector<Cephei::QL::Experimental::Credit::IDefaultProbKey^>^ Cephei::QL::Experimental::Credit::CBasket::RemainingDefaultKeys (DateTime startDate, DateTime endDate)
{
    try
    {
        QuantLib::Date _startDate = (QuantLib::Date)ValueHelper::Convert (startDate); //a
        QuantLib::Date _endDate = (QuantLib::Date)ValueHelper::Convert (endDate); //a
    	std::vector<QuantLib::DefaultProbKey> _rv = (std::vector<QuantLib::DefaultProbKey>)(*_ppBasket)->remainingDefaultKeys ( _startDate,  _endDate );   
        CoVector<Cephei::QL::Experimental::Credit::IDefaultProbKey^>^ _nrv = REFNEW CoVector<Cephei::QL::Experimental::Credit::IDefaultProbKey^>(REFNEW CDefaultProbKeyVector (_rv));
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::Core::IVector<Cephei::QL::Experimental::Credit::IRecoveryRateModel^>^ Cephei::QL::Experimental::Credit::CBasket::RemainingRecModels (DateTime startDate, DateTime endDate)
{
    try
    {
        QuantLib::Date _startDate = (QuantLib::Date)ValueHelper::Convert (startDate); //a
        QuantLib::Date _endDate = (QuantLib::Date)ValueHelper::Convert (endDate); //a
    	std::vector<boost::shared_ptr<QuantLib::RecoveryRateModel> > _rv = (std::vector<boost::shared_ptr<QuantLib::RecoveryRateModel> >)(*_ppBasket)->remainingRecModels ( _startDate,  _endDate );   
        CoVector<Cephei::QL::Experimental::Credit::IRecoveryRateModel^>^ _nrv = REFNEW CoVector<Cephei::QL::Experimental::Credit::IRecoveryRateModel^>(REFNEW CRecoveryRateModelVector (_rv, this));
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Experimental::Credit::CBasket::Lgd::get ()
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppBasket)->lgd ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Experimental::Credit::CBasket::Notional::get ()
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppBasket)->notional ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Experimental::Credit::IBasket^ Cephei::QL::Experimental::Credit::CBasket_Factory::Create (Cephei::Core::IVector<String^>^ names, Cephei::Core::IVector<Double>^ notionals, Cephei::QL::Experimental::Credit::IPool^ pool, Cephei::Core::IVector<Cephei::QL::Experimental::Credit::IDefaultProbKey^>^ defaultKeys, Cephei::Core::IVector<Cephei::QL::Experimental::Credit::IRecoveryRateModel^>^ rrModels, Microsoft::FSharp::Core::FSharpOption<Double>^ attachmentRatio, Microsoft::FSharp::Core::FSharpOption<Double>^ detachmentRatio)
{
    return REFNEW CBasket ( names,  notionals,  pool,  defaultKeys,  rrModels,  attachmentRatio,  detachmentRatio);
}
Cephei::QL::Experimental::Credit::IBasket^ Cephei::QL::Experimental::Credit::CBasket_Factory::Create ()
{
    return REFNEW CBasket ();
}
